<?php
/**
 * Created by PhpStorm.
 * User: Yan
 * Date: 2017/10/16
 * Time: 11:07
 */

namespace App\Http\Controllers\API;


use App\Http\Controllers\Controller;
use App\Models\Role;
use App\Http\Requests\RoleRequest as Request;
use Illuminate\Support\Facades\DB;

class RoleController extends Controller
{
    public function lists(Request $request)
    {
        $queryString = $request->get('query', '');
        $models = DB::table('roles')
            ->where('name', 'like', '%' . $queryString . '%')
            ->get();
        $result=[];
        if($models){
            foreach ($models as $items){
                $result[$items->id]=$items->name;
            }
        }
        return $result;
    }
    public function index(Request $request)
    {
        $queryString = $request->get('query', '');
        $models = Role::query()
            ->where('name', 'like', '%' . $queryString . '%')
            ->paginate();
        return $models;
    }
    /**
     * 添加科室
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $name = $request->get('name', '');
        $slug = $request->get('slug', '');
        $description = $request->get('description', '');
        $permission = $request->get('permission', []);
        $roleManager = new \HuangYi\Rbac\Managers\RoleManager();

        $role = $roleManager->create([
            'name' =>  $name,
            'slug' => $slug,
            'description' => $description,
        ]);
        $role->attachPermissions($permission);

        return $role;


    }


    /**
     * 修改员工
     * @param User $User
     * @param Request $request
     * @return mixed
     */
    public function update(Role $Role, Request $request)
    {
        $permission = $request->get('permission', []);
        $Role->fill($request->all());
        if ($Role->save()) {
            $roleManager = new \HuangYi\Rbac\Managers\RoleManager();

            $role = $roleManager->find($Role->id);
            DB::table('role_permission')->where('role_id',$Role->id)->delete();
            $role->attachPermissions($permission);
            return $Role;
        } else {
            return response($Role->getErrors()->toArray(), 406);
        }
    }

    protected function updatePermission($permission){

    }


    /**
     * 删除
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        try {
            $roleManager = new \HuangYi\Rbac\Managers\RoleManager();

            $deleted = $roleManager->delete($id);
            return ['state' => 'success'];
        } catch (\Exception $e) {
            return ['state' => 'failed', 'message' => $e->getMessage()];
        }
    }


    /**
     * 返回单个实例
     * @param User $user
     * @return mixed
     */
    public function show(Role $Role)
    {
        return $Role;
    }




    /**
     * @param $id
     * @return Staff
     */
    public function findModel($id)
    {

        $model = Role::findOrFail($id);
        return $model;
    }

}